package com.jtp7.demo.asyn;

import com.alibaba.fastjson.JSONObject;
import com.jtp7.demo.entity.TruckInfo;
import com.jtp7.demo.service.ICacheRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author 梅玉松
 * @project: JTP_DEMO
 * @create2021-08-12-13:47 描述:
 */
@Slf4j
@Component
public class TruckinfoAsynSave {

    //异步
    @Deprecated
    public static void asynSave(ICacheRedis<Object> iCacheRedis, String jsonString, Integer currentPage, Integer size, long miss, List<TruckInfo> records, String ... info) {

        if (ObjectUtils.isEmpty(records)) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (String s : info) {
                        iCacheRedis.redisFuzzySearchAfterTo(jsonString, currentPage, size, miss, s);
                    }
                }
            }).start();
            return;
        }
        //保存缓存时更新id
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (TruckInfo record : records) {
                    log.info("异步开始");
                    String jsonString1 = JSONObject.toJSONString(record);
                    iCacheRedis.redisFuzzySearchAfterTo(jsonString1, currentPage, size, miss, record.getId());
               /*     CompletableFuture.runAsync(() ->{
                        iCacheRedis.redisFuzzySearchAfterTo(jsonString1, currentPage, size, miss, record.getId());
                    });*/
                    log.info("22222222222");
                }
            }
        }).start();

    }

    public static void asynSave2(ICacheRedis<Object> iCacheRedis, String jsonString, Integer currentPage, Integer size, long miss, List<TruckInfo> records, String ... info) {

        if (ObjectUtils.isEmpty(records)) {
            for (String s : info) {
                CompletableFuture.runAsync(() -> iCacheRedis.redisFuzzySearchAfterTo(jsonString, currentPage, size, miss, s));

            }
            return;
        }

        //保存缓存时更新id
        for (TruckInfo record : records) {
            log.info("异步开始");
            String jsonString1 = JSONObject.toJSONString(record);

          CompletableFuture.runAsync(() ->{
              iCacheRedis.redisFuzzySearchAfterTo(jsonString1, currentPage, size, miss, record.getId());
          });
            log.info("22222222222");
        }

    }
}
